var mapStyleJSON = require('../../json/mapStyle.json');
module.exports = {
    map: null,
    init: function (id, zoom, longitude, latitude) {
        var map = new BMap.Map(id);
        var config = {
            zoom: zoom||11,
            longitude: longitude||120.093,
            latitude: latitude||30.311,
        };
        var point = new BMap.Point(config.longitude, config.latitude);
        map.addControl(new BMap.OverviewMapControl());
        map.disable3DBuilding();
        map.centerAndZoom(point, config.zoom);
        map.enableScrollWheelZoom(true);
        //map.setMapStyle({styleJson:JSON.parse(mapStyleJSON)});
        return map;
    },
    searchInit: function (idName, cb) {
        return new BMap.Autocomplete({
            input: idName,
            onSearchComplete: function (data) {
                if(cb) {
                    cb(data);
                }
            }
        });
    },
    search: function (autoObj, searchName) {
        autoObj.search(searchName);
    },
    mapSearch: function (map, searchName) {
        var myGeo = new BMap.Geocoder();
        // 将地址解析结果显示在地图上,并调整地图视野
        myGeo.getPoint(searchName, function(point){
            if (point) {
                map.centerAndZoom(point, 16);
                map.addOverlay(new BMap.Marker(point));
            }else{
                alert("您选择地址没有解析到结果!");
            }
        });
    },
    addOverlay: function (map, point, bg, size) {
        var myIcon = new BMap.Icon(bg, size);
        var marker = new BMap.Marker(point,{icon:myIcon});
        marker.setAnimation(BMAP_ANIMATION_BOUNCE);
        map.addOverlay(marker);
    }
};